package com.softaria.windows.widgets;

import java.util.HashSet;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.Widget;

/**
 * Holder for FrameResizeListeners
 * 
 * @author Roman M Kuzmin (roman@softaria.com)
 * 
 */
public class ResizeListenerRegistrator {

  private HashSet<FrameResizeListener> resizeListeners = new HashSet<FrameResizeListener>();

  public void addResizeListener(FrameResizeListener listener) {
    resizeListeners.add(listener);
  }

  public boolean removeResizeListener(FrameResizeListener listener) {
    return resizeListeners.remove(listener);
  }

  void notifyViewResized(final Widget widget) {
    DeferredCommand.addCommand(new Command() {
      public void execute() {
        for (FrameResizeListener resizeListener : resizeListeners) {
          // System.out.println("Setting width " + widget.getOffsetWidth());
          resizeListener.onResize(widget.getOffsetHeight(), widget.getOffsetWidth());
        }
      }
    });
  }
}
